package tests.multiThreadTests;

import java.util.LinkedList;

public class MySemaphore <P extends Thread, R>{
	LinkedList<R> freeResources;
	LinkedList<P> waitingThreads;
	int value;
	public MySemaphore(){

	}
	public MySemaphore(LinkedList<R> freeResources, LinkedList<P> waitingThreads, int value) {
		this.freeResources = new LinkedList<>();
		this.waitingThreads = new LinkedList<>();
		this.value = value;
	}

	public void wait(P waitingThread){
		try {
			value--;
			if (value < 0)
				waitingThread.wait();
		} catch (InterruptedException e) {
			System.out.println(waitingThread+"interruption failed");
		}
	}
	public void signal(P waitingThread){
		try {
			value++;
			if(value<=0)
				waitingThreads.getFirst().notify();
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
	}
}
